package com.zjy.alpay.config;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.tom.zx.entity.AliPayBean;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

/**
 * @author 75073
 */
@Data
public class Alipay {

    /**日志对象*/
    private static final Logger logger = LoggerFactory.getLogger(Alipay.class);

    private final String format = "json";

    /**
     * appId
     */
    private String appId = "2021000119665588";

    /**
     * 商户私钥
     */
    private String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCbtty4HjImnPl4mfWZPadC36N2CRSDuv10YFJSuUMKPfipQAjCIXmUpi37ngYxkSFseIugt3zLnODFJyK9AarLOaXm7oA1f2kLhLY4MQBdukCxGJNYK8R4fwGI92VpK8mniACNtEYEFWunUE6k5xYDl3Oe9OwWZGW8BB0g8V5uMSQ8XdQRietOnmxa9RxNuNk7XgMH5vzxU8Hd7zewosVsLQYlM8JrjrW5198ES+hPk8qwZaAvMIFEBGjuE5/T/xvCSp7Rj1y8snYgSVmuAaWBaiQH7WmCJ+iqV3p+dA9+u0I7bGXd3XnOApPwG4nGGKxuGMLZ/cLj7tbFvaRHpsdPAgMBAAECggEAN4pyD/o1STCUiz/fKlmjX2cFlwSGHLaF0QzKUFdkYjLYjwYYGaZ3B15+6FDlRKjPkzEsHJdWHApI4gmjimcd1H8rfcN0V6Cqh0E+/av8xXG5wXFSnRB/c7tZgNE9itXjh64rBGBY2PwK+hLa+lays3VB+GDEFPiakQWmnil51TODWSYD6eqkz3LpeFfFc0B3hB68ffNtcTsjX0TjAm98JFGqbeNPgjP5kVzX9yxfapFD8uZuDricJJFmkqmK0Wk44ms68pJRkOfJsXlqQGYB7w/0WeQ2PaE/GURC5rz85WJs/PjHThI8axR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uxRbiaGO0Zh2KAEg3x2Rbj6Xq5WI3/sVJLdjUlrgX+nDBhmHst+TcXB9ASAdyPspJ5wsYNTimVfSFmHpNchRtuxpfT480u0RaV388vjRAklf0QSZ7NIlNAC15eIKVwJ9H5c1xQd/FrG9qbMt4KK6bBAbbUk5V84o5vLhgQiMcNBUEbpwdUlkTG6uDRIwP8FV/+K61wQGKvqr4iTlvQpAvZLoWJVgiWThRI4xqjSt/OGupmb1dqkoB+2TwIDAQAB";

    /**
     * 服务器异步通知页面路径，需要公网能访问到
     */
    private String notifyUrl;

    /**
     * 服务器同步通知页面路径，需要公网能访问到
     */
    private String returnUrl;

    /**
     * 签名方式
     */
    private String signType = "RSA2";

    /**
     * 字符编码格式
     */
    private String charset = "utf-8";

    /**
     * 支付宝网关
     */
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(AliPayBean aliPayBean) throws AlipayApiException {

        AlipayClient alipayClient = new DefaultAlipayClient(
                gatewayUrl, appId, privateKey, format, charset, publicKey, signType);

        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(returnUrl);
        alipayRequest.setNotifyUrl(notifyUrl);
        alipayRequest.setBizContent(JSON.toJSONString(aliPayBean));
        logger.info("封装请求支付宝付款参数为:{}", JSON.toJSONString(alipayRequest));

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        alipayClient.pageExecute(alipayRequest,"get").getBody();
        logger.info("请求支付宝付款返回参数为:{}", result);

        return result;
    }
}

